package com.jingdianjichi.auth.application.controller;

import cn.dev33.satoken.annotation.SaCheckOr;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.common.base.Preconditions;
import com.jingdianjichi.auth.application.convert.AuthUserDTOConverter;
import com.jingdianjichi.auth.application.dto.AuthUserDTO;
import com.jingdianjichi.auth.common.entity.Result;
import com.jingdianjichi.auth.domain.entity.AuthUserBO;
import com.jingdianjichi.auth.domain.service.AuthUserDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user/")
@Slf4j
public class UserController {
    @Resource
    private AuthUserDomainService authUserDomainService;

    @SaCheckPermission
    @SaCheckOr
    // 测试登录，浏览器访问： http://localhost:8081/user/doLogin?username=zhang&password=123456
    @RequestMapping("doLogin")
    //前端传入从微信公众号获取的验证码进行登录
    public Result<SaTokenInfo> doLogin(String validCode) {

//        if("zhang".equals(username) && "123456".equals(password)) {
//            StpUtil.login("丁少");
//            return SaResult.data(tokenInfo());
//        }
//        return SaResult.error("登陆失败");
        try {
            Preconditions.checkArgument(!StringUtils.isBlank(validCode),"验证码不能为空");
            return Result.ok(authUserDomainService.doLogin(validCode));
        } catch (Exception e) {
            log.error("UserController.doLogin.error:{}",e.getMessage(),e);
            return Result.fail("登录失败");
        }
    }
    // 查询登录状态，浏览器访问： http://localhost:8081/user/isLogin
    @RequestMapping("isLogin")
    public String isLogin() {
        StpUtil.getPermissionList();
        return "当前会话是否登录：" + StpUtil.isLogin();
    }
    // 测试注销  ---- http://localhost:8081/acc/logout
    @RequestMapping("logout")
    public SaResult logout() {
        StpUtil.logout();
        return SaResult.ok();
    }
    //获取当前会话id
    @RequestMapping("getLoginId")
    public String getLoginId() {
        return "当前会话id：" + StpUtil.getLoginIdAsString();
    }
    // 查询 Token 信息  ---- http://localhost:8081/acc/tokenInfo
    @RequestMapping("tokenInfo")
    public SaResult tokenInfo() {
        return SaResult.data(StpUtil.getTokenInfo());
    }
//以上均为测试方法
    @PostMapping("register")
    public Result<Boolean> register(@RequestBody AuthUserDTO authUserDTO){
        if(log.isInfoEnabled()){
            log.info("注册用户信息:{}", JSON.toJSONString(authUserDTO));
        }
        try {
            //断言参数校验Precondition
            Preconditions.checkArgument(!StringUtils.isBlank(authUserDTO.getUserName()),"用户名不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(authUserDTO.getEmail()),"手机号不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(authUserDTO.getPassword()),"密码不能为空");
            AuthUserBO authUserBO= AuthUserDTOConverter.Instance.convertDTOToBO(authUserDTO);
            return Result.ok(authUserDomainService.register(authUserBO));
        } catch (Exception e) {
            log.error("UserController.register.error:{}",e.getMessage(),e);
            return Result.fail("注册失败");
        }
    }
    @PostMapping("update")
    public Result<Boolean> update(@RequestBody AuthUserDTO authUserDTO) {
        if(log.isInfoEnabled()){
            log.info("更新用户信息:{}",JSON.toJSONString(authUserDTO));
        }
        try {
            //断言参数校验Precondition
            Preconditions.checkArgument(!StringUtils.isBlank(authUserDTO.getUserName()),"用户名不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(authUserDTO.getEmail()),"手机号不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(authUserDTO.getPassword()),"密码不能为空");
            AuthUserBO authUserBO= AuthUserDTOConverter.Instance.convertDTOToBO(authUserDTO);
            return Result.ok(authUserDomainService.update(authUserBO));
        } catch (Exception e) {
            log.error("UserController.update.error:{}",e.getMessage(),e);
            return Result.fail("更新失败");
        }
    }

    @PostMapping("delete")
    public Result<Boolean> delete(@RequestBody AuthUserDTO authUserDTO) {
        if(log.isInfoEnabled()){
            log.info("删除用户信息:{}",JSON.toJSONString(authUserDTO));
        }
        try {
            //断言参数校验Precondition
            Preconditions.checkNotNull(authUserDTO.getId(),"用户名不能为空");
            AuthUserBO authUserBO= AuthUserDTOConverter.Instance.convertDTOToBO(authUserDTO);
            return Result.ok(authUserDomainService.delete(authUserBO));
        } catch (Exception e) {
            log.error("UserController.delete.error:{}",e.getMessage(),e);
            return Result.fail("删除失败");
        }
    }

    @PostMapping("changeStatus")
    public Result<Boolean> changeStatus(@RequestBody AuthUserDTO authUserDTO) {
        if(log.isInfoEnabled()){
            log.info("删除用户信息:{}",JSON.toJSONString(authUserDTO));
        }
        try {
            //断言参数校验Precondition
            Preconditions.checkNotNull(authUserDTO.getId(),"用户名不能为空");
            Preconditions.checkNotNull(authUserDTO.getStatus(),"用户状态不能为空");
            AuthUserBO authUserBO= AuthUserDTOConverter.Instance.convertDTOToBO(authUserDTO);
            return Result.ok(authUserDomainService.update(authUserBO));
        } catch (Exception e) {
            log.error("UserController.changeStatus.error:{}",e.getMessage(),e);
            return Result.fail("更新状态失败");
        }
    }

}
